package com.lovo.controller;

import com.lovo.util.SnowIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("file")
@Api(tags = "文件上传控制层")
public class FileController {

    @ApiOperation(value = "文件上传名为file，返回路径，ip+port+路径获取文件")
    @PostMapping("upload")
    public String upload(@RequestParam("file")MultipartFile file){
        //获取到后缀
        String suffix=file.getOriginalFilename();
        suffix=suffix.substring(suffix.lastIndexOf("."),suffix.length());
        //随机生成文件名
     String fileName=   SnowIdUtils.uniqueLongHex()+suffix;
     String resultPath="";
     //获取系统版本
     String str=   System.getProperties().getProperty("os.name");
        File newFile=null;
        if(str.startsWith("Win")){
            newFile=new File("D:/img/"+fileName);
            resultPath="imgwin/"+fileName;
       }else {
            newFile=new File("/mnt/img/"+fileName);
            resultPath="img/"+fileName;
       }
     //上传
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
           return "";
        }

        return resultPath;
    }

    public static void main(String[] args) {
     String str=   System.getProperties().getProperty("os.name");
        System.out.println(str);
    }
}
